//
// Created by F1997 on 2025/3/11.
//

#ifndef NETWORK_TRAFFIC_PROCESSING_SOCKETUTILS_H
#define NETWORK_TRAFFIC_PROCESSING_SOCKETUTILS_H
#include "pch.h"
#include "IniReader.h"
#include "SafeQueueTemplate.h"

#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <net/ethernet.h>
#include <netpacket/packet.h>
#include <sys/ioctl.h> // 执行设备特定的 I/O 控制操作
#include <ifaddrs.h>

#include <sys/select.h>
#include <sys/epoll.h>


namespace ntp {
    const int MAXRECV = 1518;

    class SocketUtils {
    public:
        SocketUtils(std::shared_ptr<spdlog::logger> _my_logger) {
            my_logger = _my_logger;
        };
        int readNetworkIfaceLine(std::string sectionId, std::chrono::system_clock::time_point startTime,
                                 ntp::IniReader* iniCfg, SafeQueue<MarketData>* data_queue, std::string debug);
        int readNetworkIfaceLine_epoll(std::string sectionId, std::chrono::system_clock::time_point startTime,
                                 ntp::IniReader* iniCfg, SafeQueue<MarketData>* data_queue, std::string debug);
        void get_interface_ips();
        int readNetwork(std::string sectionId, std::chrono::system_clock::time_point startTime, ntp::IniReader* iniCfg);
        int readNetwork2();
        int readNetwork3();
        int readNetwork4();


        static bool set_non_blocking(const bool b);
        static bool createRaw();
        static bool bindRaw(char* _portName);
        static int recvRaw(unsigned char* _Buffer);
        static int sendRaw(unsigned char* _Buffer, int _Length);
        bool is_valid() const { return m_sock != -1; }

    public:
        static char IfName[31];
        static struct sockaddr_ll SockaddrRawSend;

    public:
        char device[128]; // 网卡设备名
        int port;

        static int m_sock;
        struct sockaddr_in m_addr;
        struct sockaddr_in m_group_addr;  //group address
        socklen_t addr_len;
        pthread_mutex_t m_lockSend;//数据发送锁

        std::shared_ptr<spdlog::logger> my_logger;

    };


#pragma pack(push, 1)

    struct ETHHEADER // 以太网报头
    {
        char eh_dstmac[6];
        char eh_srcmac[6];
        short eh_type;
    };
    struct IPHEADER // ip报头
    {
        unsigned char h_verlen;
        unsigned char tos;
        unsigned short total_len;
        unsigned short identification;
        unsigned short frag_and_flags;
        unsigned char ttl;
        unsigned char proto;
        unsigned short checksum;
        unsigned int sourceIP;
        unsigned int destIP;
    };
    struct UDPHEADER // udp报头
    {
        unsigned short uh_srcport;
        unsigned short uh_dstport;
        unsigned short uh_len;
        unsigned short uh_chk;
    };

    typedef struct
    {
        unsigned char 	Flag;					//协议标志
        char 			TypeID;					//协议版本
        unsigned short  Length;					//包长度
        unsigned short	PacketNo;				//全0
        unsigned int	ChangeNo;				//增量编号
        short			InstrumentNo;			//合约编码
        char			InstrumentID[10];		//合约
        unsigned int	UpdateTime;				//最后更新时间(秒)
        unsigned short	UpdateMillisec;			//最后更新时间(毫秒)
        int				Volume;
        int				OpenInterest;
        int				BidVolume1;
        int				BidVolume2;
        int				BidVolume3;
        int				BidVolume4;
        int				BidVolume5;
        int				AskVolume1;
        int				AskVolume2;
        int				AskVolume3;
        int				AskVolume4;
        int				AskVolume5;
        double			LastPrice;
        double			BidPrice1;
        double			BidPrice2;
        double			BidPrice3;
        double			BidPrice4;
        double			BidPrice5;
        double			AskPrice1;
        double			AskPrice2;
        double			AskPrice3;
        double			AskPrice4;
        double			AskPrice5;
        double			Turnover;
    }Zbp05; // 5档行情

    typedef struct
    {
        ETHHEADER EthHead; // ethernet报头
        IPHEADER IpHead; // ip报头
        UDPHEADER UdpHead; // udp报头
        Zbp05 AmgBody; // 报文内容=5档行情
    }Zbp05Udp; // 完整的udp报文

#pragma pack(pop)

}

#endif //NETWORK_TRAFFIC_PROCESSING_SOCKETUTILS_H
